import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { User } from './user.entity';
import { Document } from './document.entity';
import { Chapter } from './chapter.entity';

@Entity('reading_progress')
@Index('idx_user_document', ['userId', 'documentId'], { unique: true })
export class ReadingProgress extends BaseEntity {
  @Column({
    type: 'bigint',
    comment: '用户ID',
  })
  userId: string;

  @Column({
    type: 'bigint',
    comment: '文档ID',
  })
  documentId: string;

  @Column({
    type: 'bigint',
    comment: '当前章节ID',
  })
  chapterId: string;

  @Column({
    type: 'int',
    comment: '当前章节序号',
  })
  chapterNumber: number;

  @Column({
    type: 'decimal',
    precision: 5,
    scale: 2,
    default: 0,
    comment: '阅读进度百分比',
  })
  progress: number;

  // 关联
  @ManyToOne(() => User)
  @JoinColumn({ name: 'userId' })
  user: User;

  @ManyToOne(() => Document)
  @JoinColumn({ name: 'documentId' })
  document: Document;

  @ManyToOne(() => Chapter)
  @JoinColumn({ name: 'chapterId' })
  chapter: Chapter;
}
